﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace StableDiffusionTools.Controls
{
    public partial class LabeledNumericUpDown : UserControl
    {
        //Mapped EventHandlers
        public event EventHandler ValueChanged
        {
            add { nupMain.ValueChanged += value; }
            remove { nupMain.ValueChanged -= value; }
        }

        //Properties
        [Category("Appearance")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Bindable(true)]
        public new string Text
        {
            get { return base.Text; }
            set
            {
                SetLabel(value);
                base.Text = value;
            }
        }

        [DefaultValue(typeof(Size), "220, 21")]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }

        [Category("Behavior")]
        public decimal Maximum
        {
            get { return nupMain.Maximum; }
            set { nupMain.Maximum = value; }
        }

        [Category("Behavior")]
        public decimal Minimum
        {
            get { return nupMain.Minimum; }
            set { nupMain.Minimum = value; }
        }

        [Category("Behavior")]
        public decimal Increment
        {
            get { return nupMain.Increment; }
            set { nupMain.Increment = value; }
        }


        [Category("Behavior")]
        public int DecimalPlaces
        {
            get { return nupMain.DecimalPlaces; }
            set { nupMain.DecimalPlaces = value; }
        }

        [Category("Behavior")]
        public decimal Value
        {
            get { return nupMain.Value; }
            set { nupMain.Value = value; }
        }

        public int NumericUpDownWidth
        {
            get { return nupMain.Width; }
            set
            {
                if (value < 0)
                {
                    value = -value;
                    nupMain.Location = new Point(value, 0);
                    nupMain.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
                    value = Width - value;
                }
                nupMain.Width = value;
            }
        }

        public AnchorStyles NumericUpDownAnchor
        {
            get { return nupMain.Anchor; }
            set { nupMain.Anchor = value; }
        }

        private IPalette _palette;

        //Constructor
        public LabeledNumericUpDown()
        {
            InitializeComponent();

            _palette = KryptonManager.CurrentGlobalPalette;

            if (_palette != null)
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            KryptonManager.GlobalPaletteChanged += new EventHandler(OnGlobalPaletteChanged);
            lblMain.ForeColor = _palette?.GetContentShortTextColor1(PaletteContentStyle.LabelNormalControl, Enabled ? PaletteState.Normal : PaletteState.Disabled) ?? lblMain.ForeColor;
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            Invalidate();
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
                _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            _palette = KryptonManager.CurrentGlobalPalette;

            lblMain.ForeColor = _palette?.GetContentShortTextColor1(PaletteContentStyle.LabelNormalControl, Enabled ? PaletteState.Normal : PaletteState.Disabled) ?? lblMain.ForeColor;

            if (_palette != null)
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            Invalidate();
        }

        //public override Size GetPreferredSize(Size proposedConstraints)
        //{
        //    proposedConstraints.Width = lblMain.Width + nupMain.Width;
        //    return proposedConstraints;
        //}
        //Methods
        private void SetLabel(string Text)
        {
            if (lblMain == null)
                return;

            lblMain.Text = Text;
        }
    }
}
